473,427 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,427 software developers and data experts.

Adding Value to Dropdownlist

In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)

Sep 7 '06 #1
5 17151
Mike

Try

ddlGroup.Items.Clear()
ddlGroup.Items.Add("Select Company")
ddlGroup.AppendDataBoundItems = True

'The key is the appendDataBoundItems this will allow the added item to stay
when
the data source is bound to the dropdownlist it works best if you add the
item before the databinding so it is the first item.

'Create your datasource here
ddlGroup.DataSource = MyDatasource
ddlGroup.DataBind()
--
Jerry
"Mike" wrote:
In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)
Sep 7 '06 #2
Another way, besides Jerry's:

After the DataBining (you may bind the data all at once by calling
Page.DataBinding() or call DataBinding on each control), you simply insert a
new ListItem at beginning of the list (Index=0):

''Bind data to the dropdownlist first
DropDownList_companies.DataSource=theSource
DropDownList_companies.DataBinding()

Dim NewItem As ListItem = New ListItem("Select Company", "0")
DropDownList_companies.Items.Insert(0, NewItem)
"Jerry C" <je*****@nospam.nospamwrote in message
news:89**********************************@microsof t.com...
Mike

Try

ddlGroup.Items.Clear()
ddlGroup.Items.Add("Select Company")
ddlGroup.AppendDataBoundItems = True

'The key is the appendDataBoundItems this will allow the added item to
stay
when
the data source is bound to the dropdownlist it works best if you add the
item before the databinding so it is the first item.

'Create your datasource here
ddlGroup.DataSource = MyDatasource
ddlGroup.DataBind()
--
Jerry
"Mike" wrote:
>In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I
would
like to add the value "Select Company" which will be the selected value
when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in
the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the
dropdownlist.
How do I add a value to the dropdownlist that is not in the database?
Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)

Sep 7 '06 #3
Thank for the reply Jerry and Norman.

I'm trying to do this the ASP.NET 2.0 way by not binding in the code behind
file. I'm using the sqldatasource control (asp:SqlDataSource) in the aspx
file. Can this be done without binding in the code behind file? Thanks.

"Jerry C" wrote:
Mike

Try

ddlGroup.Items.Clear()
ddlGroup.Items.Add("Select Company")
ddlGroup.AppendDataBoundItems = True

'The key is the appendDataBoundItems this will allow the added item to stay
when
the data source is bound to the dropdownlist it works best if you add the
item before the databinding so it is the first item.

'Create your datasource here
ddlGroup.DataSource = MyDatasource
ddlGroup.DataBind()
--
Jerry
"Mike" wrote:
In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)
Sep 7 '06 #4
This is the solution I used:

http://weblogs.asp.net/scottgu/archi...29/436804.aspx
"Mike" wrote:
In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)
Sep 7 '06 #5
The first example using "AppendDataBoundItems" can be done in the Page_Load on a dropdownlist that uses SqlDataSource
Sep 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hi, I'm building a maintenance form for a table and some of the fields are textboxes (i.e name) and some should be dropdowns (i.e country of origin) When a user clicks 'Edit' in the...
1
by: strout | last post by:
Is it possible to add a dropdownlist to pager area so that user can select how many records to display on a page? Thank Strout
2
by: Manny Chohan | last post by:
I have a dropdown bound to database. I need to add Select one to the first index of drop down list. How can i do this? Thanks Manny
3
by: bmntech | last post by:
I want to create a client app, adding value to and building on my Web app. The user of this client app will be able to seamlessly access the public web site, but will also have additional...
9
by: Lammert | last post by:
Good morning, I create an ASP.NET 2.0 web application. The situation: 1. One masterpage where the users can select an organisation in a DropDownList. 2. Different content pages. I will get...
0
by: den 2005 | last post by:
Hi everybody, I created a Gridview with a TemplateField and there is Label control in ItemTemplate and a DropdownList control in EditItemTemplate, I was to displayed them ok when I click the...
0
by: Luqman | last post by:
Which event of Datagrid is fired when i click on a Datagrid New Row. I want to add a LineNo Value to its cell no. 5 ? I tried UserAddedRows Event but its not working ? How can I know, if the...
0
by: saiprex | last post by:
I have DropDownList control which is binded to SqlDataSource. The SqlDataSource gets the values from TABLE, it all works great. But how can i set the selected value for the DropDownList? The table is...
1
by: libish | last post by:
can we add a value to a list item?? i got a list with some list members here can we add a value to that list item??? ie. suppose a list contains items like "item1" "item2" "item3" etc...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.